package cn.yl.order.server.service.impl;

import cn.yl.order.server.entity.PayOrder;
import cn.yl.order.server.entity.UserCourseOrder;
import cn.yl.order.server.mapper.PayOrderMapper;
import cn.yl.order.server.service.PayOrderService;
import cn.yl.order.server.service.UserCourseOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 支付订单信息表 服务实现类
 * </p>
 *
 * @author yl
 * @since 2022-04-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements PayOrderService {

    @Autowired
    private UserCourseOrderService userCourseOrderService;

    @Override
    public PayOrder updateByStatus(String orderId, Integer status) {
        boolean flag = false;
        QueryWrapper<PayOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        PayOrder payOrder = this.getOne(wrapper);
        Integer courseOrderStatus = 0;
        switch (status) {
            case 1:
                courseOrderStatus = 10;
                break;
            case 2:
                courseOrderStatus = 20;
                break;
            case 3:
                courseOrderStatus = 30;
                break;
            case -1:
                courseOrderStatus = 40;
                break;
            default:
                break;
        }
        if (payOrder == null) {
            return null;
        }
        payOrder.setStatus(status).setPayTime(LocalDateTime.now());
        flag = this.updateById(payOrder);
        UpdateWrapper<UserCourseOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", orderId);
        updateWrapper.set("status", courseOrderStatus);
        flag = userCourseOrderService.update(updateWrapper);
        return payOrder;
    }

    @Override
    public BigDecimal getCurrentPrice(String orderId) {
        QueryWrapper<PayOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        PayOrder payOrder = this.getOne(wrapper);
        return payOrder.getAmount();
    }

    @Override
    public PayOrder getPayOrder(String orderId) {
        QueryWrapper<PayOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        PayOrder payOrder = this.getOne(wrapper);
        return payOrder;
    }


}
